package com.knight.mall.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.knight.mall.common.pageUtils.PageResult;
import com.knight.mall.exception.ServiceException;
import com.knight.mall.mappers.PanicBuyingProductMapper;
import com.knight.mall.model.PanicBuyingProduct;
import com.knight.mall.model.Product;
import com.knight.mall.model.ShopPanicBuying;
import com.knight.mall.query.PanicBuyingProductQuery;
import com.knight.mall.query.ProductQuery;
import com.knight.mall.query.ShopPanicBuyingQuery;
import com.knight.mall.service.PanicBuyingProductService;
import com.knight.mall.service.ProductService;
import com.knight.mall.service.ShopPanicBuyingService;


@Service(value="panicBuyingProductService")
public class PanicBuyingProductServiceImpl implements PanicBuyingProductService{

	
	@Autowired
	private PanicBuyingProductMapper panicBuyingProductMapper;
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private ShopPanicBuyingService shopPanicBuyingService;
	
	@Override
	public PanicBuyingProduct load(Long id) {
		return panicBuyingProductMapper.load(id);
	}

	@Override
	public Long add(PanicBuyingProduct t) throws ServiceException {
		panicBuyingProductMapper.insert(t);
		return 1L;
	}

	@Override
	public int update(PanicBuyingProduct t) {
		panicBuyingProductMapper.update(t);
		return 0;
	}

	@Override
	public int delete(Long id) {
		return panicBuyingProductMapper.delete(id);
	}

	@Override
	public List<PanicBuyingProduct> queryList(PanicBuyingProductQuery q) {
		return panicBuyingProductMapper.queryList(q);
	}

	@Override
	public int queryCount(PanicBuyingProductQuery q) {
		return panicBuyingProductMapper.queryCount(q);
	}
	
	
	/***
	 * 查询列表
	 * 
	 * @param q
	 * @return
	 */
	public PageResult<PanicBuyingProduct> queryListForPage(PanicBuyingProductQuery q){
		
		ShopPanicBuyingQuery shopPanicBuyingQuery = new ShopPanicBuyingQuery();
		
		shopPanicBuyingQuery.setStartTimeFrom(q.getStartTime());
		
		List<ShopPanicBuying> ss= shopPanicBuyingService.queryList(shopPanicBuyingQuery);
		
		String nowStatus = "1";
		
		if(ss!=null && ss.size()>0){
			ShopPanicBuying s = ss.get(0);
			Date now = new Date();
			if (now.compareTo(s.getStartTime()) >= 0 && now.compareTo(s.getEndTime()) <= 0) {
				// 在中间
				nowStatus = "2";
			} else if (s.getEndTime().compareTo(now) < 0) {
				// 结束
				nowStatus = "1";
			} else if (s.getStartTime().compareTo(now) > 0) {
				// 即将开始
				nowStatus = "3";
			}
		}
		
		
		List<PanicBuyingProduct> data = this.queryList(q);
		
		if(data!=null && data.size()>0){
			for(PanicBuyingProduct p:data){
				
				Integer num = p.getNum();
				Integer saleNum = p.getSaleNum();
				if (num == null || num == 0) {
					p.setShowNum("100%");
				} else {
					p.setShowNum(saleNum.toString());
				}
				
				ProductQuery pQuery = new ProductQuery();
				pQuery.setIdArray(p.getProductId());
				List<Product> listPS = productService.queryList(pQuery);
				Product pt = null;
				if (listPS != null && listPS.size() > 0) {
					pt = listPS.get(0);
				}
				p.setProduct(pt);
				p.setNowStatus(nowStatus);
			}
		}
		
		
		
		int count = this.queryCount(q);
		return PageResult.create(q, data, count);
	}
	
	
	
}
